#!/bin/bash
# 阿里宝宝电商平台 - 快速启动脚本（简化版）

echo "=========================================="
echo "阿里宝宝电商平台 - Web服务器启动"
echo "=========================================="
echo ""

# 获取脚本所在目录
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

# 检查Python环境
if ! command -v python3 &> /dev/null; then
    echo "❌ 错误: 未找到Python3，请先安装Python3"
    exit 1
fi

echo "✓ Python3 已安装: $(python3 --version)"
echo ""

# 检查端口是否被占用
if lsof -Pi :8000 -sTCP:LISTEN -t >/dev/null 2>&1 ; then
    echo "⚠️  警告: 端口8000已被占用"
    echo "正在尝试关闭占用端口的进程..."
    lsof -ti:8000 | xargs kill -9 2>/dev/null
    sleep 2
    echo "✓ 端口已释放"
    echo ""
fi

# 检查是否需要安装依赖
if [ -f "requirements.txt" ]; then
    echo "正在检查依赖包..."
    python3 -c "import flask" 2>/dev/null
    if [ $? -ne 0 ]; then
        echo "正在安装依赖包..."
        pip3 install -r requirements.txt -q
        echo "✓ 依赖安装完成"
    else
        echo "✓ 依赖包已安装"
    fi
    echo ""
fi

echo "=========================================="
echo "正在启动Web服务器..."
echo "=========================================="
echo "服务器地址: http://localhost:8000"
echo "主页: http://localhost:8000/index.html"
echo "订单页面: http://localhost:8000/orders.html"
echo ""
echo "提示: 按 Ctrl+C 停止服务器"
echo "=========================================="
echo ""

# 启动服务器
python3 server-simple.py
